package com.seabook.happy.website.web.vo;

import java.util.ArrayList;
import java.util.List;

public class MenuNode {
	private String txt;
	private String url;
	private final List<MenuNode> subMenus;

	public MenuNode(String txt, String url) {
		this.txt = txt;
		this.url = url;
		this.subMenus = new ArrayList<MenuNode>();
	}

	public void addSubMenu(MenuNode node) {
		subMenus.add(node);
	}

	public String getTxt() {
		return txt;
	}

	public void setTxt(String txt) {
		this.txt = txt;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<MenuNode> getSubMenus() {
		return subMenus;
	}

	public static MenuNode getDummyMenu() {
		MenuNode topNode = new MenuNode("Archive", "/");
		MenuNode node2011 = new MenuNode("2011", "/");
		MenuNode node2010 = new MenuNode("2010", "/");
		MenuNode nodeJan2011 = new MenuNode("Jan", "/");
		MenuNode nodeMay2011 = new MenuNode("May", "/");
		MenuNode nodeJun2011 = new MenuNode("Jun", "/");
		MenuNode nodeJan2010 = new MenuNode("Jan", "/");
		MenuNode nodeMay2010 = new MenuNode("May", "/");
		MenuNode nodeJun2010 = new MenuNode("Jun", "/");
		topNode.addSubMenu(node2011);
		topNode.addSubMenu(node2010);
		node2011.addSubMenu(nodeJun2011);
		node2011.addSubMenu(nodeJan2011);
		node2011.addSubMenu(nodeMay2011);
		node2010.addSubMenu(nodeJun2010);
		node2010.addSubMenu(nodeJan2010);
		node2010.addSubMenu(nodeMay2010);
		nodeJun2011.addSubMenu(new MenuNode("F1", "http://localhost:8888/showBlogArticle.jsp?id=agxoYXBweXNlYWJvb2tyDwsSCUJfQXJ0aWNsZRgIDA"));
		nodeJun2011.addSubMenu(new MenuNode("test", "http://localhost:8888/showBlogArticle.jsp?id=agxoYXBweXNlYWJvb2tyDwsSCUJfQXJ0aWNsZRgHDA"));
		nodeJun2011.addSubMenu(new MenuNode("test1", "url"));
		nodeJun2011.addSubMenu(new MenuNode("test2", "url"));
		nodeJun2011.addSubMenu(new MenuNode("Article 5", "url"));
		nodeMay2011.addSubMenu(new MenuNode("F1 1", "http://localhost:8888/showBlogArticle.jsp?id=agxoYXBweXNlYWJvb2tyDwsSCUJfQXJ0aWNsZRgIDA"));
		nodeMay2011.addSubMenu(new MenuNode("test dd", "http://localhost:8888/showBlogArticle.jsp?id=agxoYXBweXNlYWJvb2tyDwsSCUJfQXJ0aWNsZRgHDA"));
		nodeJun2010.addSubMenu(new MenuNode("Article 3", "url"));
		nodeJan2011.addSubMenu(new MenuNode("Article 4", "url"));
		nodeMay2010.addSubMenu(new MenuNode("Article 5", "url"));
		return topNode;
	}

	public String toHtml() {
		StringBuffer sb = new StringBuffer();
		sb.append("\n<li>");
		if (subMenus.size() > 0) {
			sb.append("    <a href='javascript:void(0);' ");
			sb.append("childid = 't" + (this.hashCode()) + "'");
			sb.append(" class='cat_close category'>&nbsp;&nbsp;&nbsp;&nbsp;</a>");
			sb.append("<a href='javascript:void(0);'>" + txt + "("
					+ subMenus.size() + ")</a>");
		} else {
			sb.append("    <a href='"+url+"' ");
			sb.append(" class='product'>&nbsp;&nbsp;&nbsp;&nbsp;</a>");
			sb.append("<a href='"+url+"'>" + txt + "("
					+ subMenus.size() + ")</a>");
		}
		sb.append("</li>");

		if (subMenus.size() > 0) {

			sb.append("\n<ul id='t" + this.hashCode() + "'>");
			for (MenuNode node : subMenus) {
				sb.append(node.toHtml());
			}
			sb.append("</ul>");
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		MenuNode node = MenuNode.getDummyMenu();
		System.out.println(node.toHtml());
	}

}
